﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dhtl.Service.IService;
using dai_hoc_tuong_lai.Areas.Admin.Models;
using DataBase.Daihoctuonglai;
using FarmeWork.Helper;
using Comom.Project.ControllerCom;

namespace dai_hoc_tuong_lai.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class RolesController : UTController
    {
        //
        // GET: /Admin/Roles/
        private readonly IRoles _iRoles;

        public RolesController(IRoles iRoles)
        {
            this._iRoles = iRoles;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {

            var listEntity = _iRoles.List();
            List<RolesModel> model = new List<RolesModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    RolesModel ml = new RolesModel();
                    RolesModel.MapForm(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            return PartialView("_List", model);
        }

        public ActionResult Add()
        {
            RolesModel model = new RolesModel();
            model.Id = 0;
            model.Datecreate = DateTime.Now;
            model.accessAdmin = false;
            model.Perssiom = 1;

            return PartialView("_Add", model);
        }

        public ActionResult Update(int id)
        {
            RolesModel model = new RolesModel();
            var entity = _iRoles.GetById(id);
            try
            {
                RolesModel.MapForm(entity, ref model);
            }
            catch { }

            return PartialView("_Update", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(RolesModel model)
        {

            try
            {
                if (string.IsNullOrEmpty(model.Title))
                {
                    return JsonError("Nhập tên roles vào");
                }

                if (string.IsNullOrEmpty(model.Name))
                {
                    return JsonError("Nhập mã roles vào");
                }


                var entity = new Role();
                RolesModel.MapForm(model, ref entity);
                _iRoles.addOrupdate(entity);

                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _iRoles.delete(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

    }
}
